package cn.ekuma.pos.pda.inventory;

import java.util.HashMap;
import java.util.List;
import com.openbravo.bean.Product;
import com.openbravo.bean.ProductAuxiliar;
import com.openbravo.bean.ProductBom;
import com.openbravo.bean.ProductCode;
import com.openbravo.bean.ProductCustomerCategoryPrice;
import com.openbravo.bean.ProductPrice;
import com.openbravo.bean.ProductScore;
import com.openbravo.bean.ProductStockLevel;
import com.openbravo.data.basic.BasicException;
import android.app.ProgressDialog;
import android.view.View;
import android.view.ViewGroup;
import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.data.dao.bean.I_ViewBean;
import cn.ekuma.data.ui.android.AbstractViewBeanDetailActivate;
import cn.ekuma.epos.bean.util.ProductInfoHelper;
import cn.ekuma.epos.datalogic.DataLogicPDA;

public class ProductDetailViewActivate extends AbstractViewBeanDetailActivate<Product>{

	
	public static final String[] GROUPNAMES={
		"包内商品",
		"会员分类价",
		"一品多码",
		"一品多码",
		"相似商品",
		"库存上下限",
		"价格",
		"积分"
	};
	
	protected void initData() {
		final ProgressDialog dialog = ProgressDialog.show(this, "", 
		        "查旬中，请稍等 …", true, true);
		new Thread(){
			@SuppressWarnings({ "unchecked"})
			
			public void run(){
				I_DataLogic dlERP=DataLogicPDA.getDataLogic();
				HashMap<String,List<I_ViewBean>> groupLinkData=new HashMap<String,List<I_ViewBean>>();
				String groupName1;
				try {
					List<I_ViewBean> lists=dlERP.listWithParentClassAndKey(ProductBom.class.getName(), Product.class.getName(),obj.getID());
					groupName1="包内商品";
					if(lists!=null&&!lists.isEmpty()){
						
						groupLinkData.put(groupName1, lists);
					}

					lists=dlERP.listWithParentClassAndKey(ProductCustomerCategoryPrice.class.getName(), Product.class.getName(),obj.getID());
					groupName1="会员分类价";
					if(lists!=null&&!lists.isEmpty()){
						
						groupLinkData.put(groupName1, lists);
					}
					
					lists=dlERP.listWithParentClassAndKey(ProductCode.class.getName(), Product.class.getName(),obj.getID());
					groupName1="一品多码";
					if(lists!=null&&!lists.isEmpty()){
						
						groupLinkData.put(groupName1, lists);
					}

					lists=dlERP.listWithParentClassAndKey(ProductAuxiliar.class.getName(), Product.class.getName(),obj.getID());
					groupName1="相似商品";
					if(lists!=null&&!lists.isEmpty()){
						
						groupLinkData.put(groupName1, lists);
					}
					
					lists=dlERP.listWithParentClassAndKey(ProductStockLevel.class.getName(), Product.class.getName(),obj.getID());
					groupName1="库存上下限";
					if(lists!=null&&!lists.isEmpty()){
						
						groupLinkData.put(groupName1, lists);
					}
					
					lists=dlERP.listWithParentClassAndKey(ProductPrice.class.getName(), Product.class.getName(),obj.getID());
					groupName1="价格";
					if(lists!=null&&!lists.isEmpty()){
						groupLinkData.put(groupName1, lists);
					}		
					
					lists=dlERP.listWithParentClassAndKey(ProductScore.class.getName(), Product.class.getName(),obj.getID());
					groupName1="积分";
					if(lists!=null&&!lists.isEmpty()){
						groupName.add(groupName1);
						groupLinkData.put(groupName1, lists);
					}
					mHandler.obtainMessage(MSG_SUCCESS,groupLinkData).sendToTarget();
				} catch (BasicException e) {
					mHandler.obtainMessage(MSG_FAILURE).sendToTarget();
				}
				dialog.dismiss(); 
			}
			}.start();		
	}
	
	
	public View getVeiwBeanDetailView(Product obj) {
		return createTextView(ProductInfoHelper.getProductDetial(obj));
	}

	
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		String groupName1=groupName.get(groupPosition);
		int realPosition=getRealPosition(GROUPNAMES,groupName1);
		I_ViewBean obj=groupLinkData.get(groupName1).get(childPosition);
		View retView=null;
		switch(realPosition){
		 case 0://"包内商品":
			ProductBom productBom=(ProductBom)obj;
			retView=createTextView(productBom.getIncludeProduct().getName());
			break;
		case 1://"会员分类价":
			ProductCustomerCategoryPrice temp=(ProductCustomerCategoryPrice)obj;
			retView=createTextView(temp.getCustomerCategory().getName()+":"+temp.getM_dCustomerCategoryPrice());
			break;
		case 2://"一品多码":
			ProductCode productCode=(ProductCode)obj;
			retView=createTextView(productCode.getCode());
			break;
		case 3://"相似商品":
			ProductAuxiliar productAuxiliar=(ProductAuxiliar)obj;
			retView=createTextView(productAuxiliar.getProduct().getName());
		  break;
		case 4://"库存上下限":
			ProductStockLevel productStockLevel=(ProductStockLevel)obj;
			retView=createTextView(productStockLevel.getLocation()+":"+productStockLevel.getStockSecurity()+":"+productStockLevel.getStockMax());
			break;
		case 5://"价格":
			ProductPrice productPrice=(ProductPrice)obj;
			retView=createTextView(productPrice.getGroupName()+":"+productPrice.getM_dCustomerPrice()+":"+productPrice.getM_dPriceSell());
			break;
		case 6://"积分":
			ProductScore productScore=(ProductScore)obj;
			retView=createTextView(productScore.getType()+":"+productScore.getScore());
			break;
		}
		return retView;
	}
	
}
